﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

namespace MyBlog2.Models
{
    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options)
            :base(options)
        { 
            
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            //使用x.ArticleId, x.TagId生成 ArticleTag的联合主键
            //执行数据库迁移
            modelBuilder.Entity<ArticleTag>().HasKey(x => new { x.ArticleId, x.TagId });
            //配置多对多 ，就是两个一对多
            modelBuilder.Entity<ArticleTag>().HasOne(x => x.Article).WithMany(x => x.ArticleTag).HasForeignKey(x => x.ArticleId);
            modelBuilder.Entity<ArticleTag>().HasOne(x => x.Tag).WithMany(x => x.ArticleTag).HasForeignKey(x => x.TagId);



            ////通过fluent api指定了title是key属性，用来update和delete数据
            //modelBuilder.Entity<Article>(entity =>
            //{
            //    entity.Property(e => e.Id).ValueGeneratedOnAdd();
            //    entity.HasKey(e => e.Id);    //声明列Title是实体的Key属性
            //});
        }

        public DbSet<Article> Articles { get; set; }
        public DbSet<ArticleClassify> ArticleClassifys { get; set; }
        public DbSet<Tag> Tags { get; set; }
        public DbSet<Comment> ArticleComments { get; set; }
        public DbSet<FriendLink> FriendLinks { get; set; }
        public DbSet<User> Users { get; set; }

        public DbSet<ArticleTag> ArticleTags { get; set; }

    }
}
